import Vue from "vue";
import AppLoading from "./app-loading.vue";

export default class Loading {

    /**
     * 实例对象
     *
     * @private
     * @static
     * @memberof Loading
     */
    private static loading: any;

    /**
     * vue 实例
     *
     * @private
     * @type {Vue}
     * @memberof Loading
     */
    private vm!: Vue;


    /**
     * 单例构造函数
     * 
     * @memberof Loading
     */
    private constructor() {
        if (Loading.loading) {
            return Loading.loading;
        }
    }

    /**
     * 静态获取单例对象方法
     *
     * @static
     * @returns {Loading}
     * @memberof Loading
     */
    public static getInstance(): Loading {
        if (!Loading.loading) {
            Loading.loading = new Loading();
        }
        return Loading.loading;
    }

    /**
     * 开始加载
     *
     * @param {{ message?: string, type?: string, color?: string, size?: string }} props
     * message:加载信息 type:加载类型(circular,spinner) color:颜色 size:尺寸
     * @returns
     * @memberof Loading
     */
    public startLoading(props: { message?: string, type?: string, color?: string, size?: string }) {
        let component = AppLoading;
        const vm = new Vue({
            render(h) {
                return h(component, { props });
            }
        }).$mount();
        this.vm = vm;
        document.body.appendChild(vm.$el);
        const comp: any = vm.$children[0];
        comp.start();
    }

    /**
     * 结束加载
     *
     * @memberof Loading
     */
    public endLoading() {
        const comp: any = this.vm.$children[0];
        comp.end();
    }

}